// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
const _ = db.command
// 云函数入口函数
exports.main = async(event, context) => {
  //定义数组删除元素的函数
  Array.prototype.remove = function(val) {
    var index = this.indexOf(val);
    if (index > -1) {
      this.splice(index, 1);
    }
  };

  const wxContext = cloud.getWXContext()
  let openid = wxContext.OPENID;
  const {
    t1,
    t2,
    op
  } = event;


  if (t1 == 'run') {
    try{
      switch (t2) {
        case 'day':
          let pro = await db.collection('stepPhb').where({
            openid: op,
            dup: openid
          }).field({
            dup: true
          }).get();
          if (pro.data.length > 0) {
            //取消点赞
            let dup = pro.data[0].dup;
            dup.remove(openid);
            await db.collection('stepPhb').where({
              openid:op,
              dup: openid
            }).update({
              data: {
                dup: dup
              }
            })
          } else {
            //点赞
            await db.collection('stepPhb').where({
              openid: op
            }).update({
              data: {
                dup: _.unshift(openid)
              }
            })
          }
          break;

        case 'week':
          let pro1 = await db.collection('stepPhb').where({
            openid: op,
            wup: openid
          }).field({
            wup: true
          }).get();
          if (pro1.data.length > 0) {
            //取消点赞
            let wup = pro1.data[0].wup;
            wup.remove(openid);
            await db.collection('stepPhb').where({
              openid:op,
              wup: openid
            }).update({
              data: {
                wup: wup
              }
            })
          } else {
            //点赞
            await db.collection('stepPhb').where({
              openid: op
            }).update({
              data: {
                wup: _.unshift(openid)
              }
            })
          }
          break;

        case 'month':
          let pro2 = await db.collection('stepPhb').where({
            openid: op,
            mup: openid
          }).field({
            mup: true
          }).get();
          if (pro2.data.length > 0) {
            //取消点赞
            let mup = pro2.data[0].mup;
            mup.remove(openid);
            await db.collection('stepPhb').where({
              openid:op,
              mup: openid
            }).update({
              data: {
                mup: mup
              }
            })
          }
          else {
            //点赞
            await db.collection('stepPhb').where({
              openid: op
            }).update({
              data: {
                mup: _.unshift(openid)
              }
            })
          }
          break;
      }
      return{
        code:0,
        mes:'点赞/取消点赞成功'
      }
    }catch(e){
      return {
        code:1,
        mes:'操作失败'
      }
    }
  } 
  
  else {
    try {
      switch (t2) {
        case 'day':
          let pro = await db.collection('giftPhb').where({
            openid: op,
            dup: openid
          }).field({
            dup: true
          }).get();
          if (pro.data.length > 0) {
            //取消点赞
            let dup = pro.data[0].dup;
            dup.remove(openid);
            await db.collection('giftPhb').where({
              openid: op,
              dup: openid
            }).update({
              data: {
                dup: dup
              }
            })
          } else {
            //点赞
            await db.collection('giftPhb').where({
              openid: op
            }).update({
              data: {
                dup: _.unshift(openid)
              }
            })
          }
          break;

        case 'week':
          let pro1 = await db.collection('giftPhb').where({
            openid: op,
            wup: openid
          }).field({
            wup: true
          }).get();
          if (pro1.data.length > 0) {
            //取消点赞
            let wup = pro1.data[0].wup;
            wup.remove(openid);
            await db.collection('giftPhb').where({
              openid: op,
              wup: openid
            }).update({
              data: {
                wup: wup
              }
            })
          } else {
            //点赞
            await db.collection('giftPhb').where({
              openid: op
            }).update({
              data: {
                wup: _.unshift(openid)
              }
            })
          }
          break;

        case 'month':
          let pro2 = await db.collection('giftPhb').where({
            openid: op,
            mup: openid
          }).field({
            mup: true
          }).get();
          if (pro2.data.length > 0) {
            //取消点赞
            let mup = pro2.data[0].mup;
            mup.remove(openid);
            await db.collection('giftPhb').where({
              openid: op,
              mup: openid
            }).update({
              data: {
                mup: mup
              }
            })
          }
          else {
            //点赞
            await db.collection('giftPhb').where({
              openid: op
            }).update({
              data: {
                mup: _.unshift(openid)
              }
            })
          }
          break;
      }
      return {
        code: 0,
        mes: '点赞/取消点赞成功'
      }
    } catch (e) {
      return {
        code: 1,
        mes: '操作失败'
      }
    }
  }
  return {}
}